<template>
   <a-row id="globalHeader" class="grid-demo"  align="center" :wrap="false" >
    <a-col flex="auto">
    <a-menu mode="horizontal" :selected-keys="selectedKeys" @menu-item-click="doMenuClick">
      <a-menu-item key="0" :style="{ padding: 0, marginRight: '38px' }" disabled>
        <div class="title-bar">
          <img class="logo" src="../assets/logo.png"/>
          <div class="title">赛OJ</div>
        </div>
      </a-menu-item>
      <a-menu-item v-for="item in visibleRoutes" :key="item.path">
        {{ item.name }}
      </a-menu-item>
    </a-menu>
    </a-col>
    <a-col flex="100px">
      <div>{{store.state.user.loginUser.userName}}</div>
    </a-col>
  </a-row>
  
</template>

<script setup lang="ts">
import checkAccess from '@/access/checkAccess';
import {routes} from '../router/routes'
import { computed, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex';
import ACCESS_ENUM from '@/access/accessEnum';

const router=useRouter();
const store=useStore();
//展示在菜单的路由
//使用计算属性动态的根据权限展示菜单
const visibleRoutes=computed(()=>{
  return  routes.filter((item,index)=>{
  if(item.meta?.hideInMenu){
    return false;
  }
  // 根据权限校验过滤菜单
    if(!checkAccess(store.state.user.loginUser,item?.meta?.access as string)){
      return false;
    }
  return true;
  });
});
// 默认主页
const selectedKeys=ref(['/']);

//路由跳转时,更新选中的菜单项
router.afterEach((to,from,failure)=>{
  selectedKeys.value=[to.path];
});

const doMenuClick=(key:string)=>{
  router.push({
    path:key,
  });
};

// setTimeout(()=>{
//   store.dispatch("user/getLoginUser",{
//     userName:"搜一码赛",
//     userRole:ACCESS_ENUM.ADMIN,
//   });
// },3000);
</script>


<style scoped> 
.title-bar{
  display: flex;
  align-items: center;
}
.title{
  color: #444;
  margin-left: 16px;
}
.logo{
  height: 48px;
}
</style>
